package com.topsoft.email.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mvc.core.utils.SerializeUtil;
import com.mvc.core.utils.javamail.EmailSender;
import com.mvc.core.utils.javamail.JavaMailSenderInfo;
import com.topsoft.email.dao.IEmailLogDao;
import com.topsoft.email.dao.IMailUserDao;
import com.topsoft.email.model.MailUser;
import com.topsoft.email.service.IEmailSenderService;
import com.topsoft.email.util.bean.AjaxJson;
import com.topsoft.email.util.bean.MailParamBean;
import com.topsoft.email.util.mail.JoddMailSender;
import com.topsoft.email.util.mail.SendCloudMailUtil;


@Service
public class EmailSenderServiceImpl implements IEmailSenderService{
	
	@Resource
	private IMailUserDao  mailUserDao;
	
	@Resource
	private IEmailLogDao  emailLogDao;
	

	@Override
	public String sendMail() {
		System.out.println("发送邮件");
		AjaxJson json=new AjaxJson();
		json.setMsg("你好哇");
		return SerializeUtil.fastJsonSerialize(json);
	}

	
	
	/**
	 * 发送即时邮件
	 */
	@Override
	public AjaxJson sendJoddMail(MailParamBean paramBean) {
		AjaxJson json=new AjaxJson();
		
		
		MailUser user=mailUserDao.findById(paramBean.getMailUserId());
		
		paramBean.setSmtpUserName(user.getSmtpUserName());
		paramBean.setSmtpPassword(user.getSmtpPassword());
		paramBean.setProtocol(user.getHost());
		paramBean.setPort(user.getPort());
		
		
		paramBean.setSendTime(new Date());
		
		//发邮件
		Boolean flag=JoddMailSender.sendMail(paramBean);
		json.setSuccess(flag);
		return json;
	}



	@Override
	public AjaxJson sendJavaMail(MailParamBean paramBean) {
        AjaxJson json=new AjaxJson();
		
		MailUser user=mailUserDao.findById(paramBean.getMailUserId());
		
		JavaMailSenderInfo  info=new JavaMailSenderInfo();
		/**
		 * 发送者信息
		 */
		info.setHost(user.getHost());
		info.setPort(user.getPort());
		info.setUser(user.getSmtpUserName());
		info.setPassword(user.getSmtpPassword());
	    /**
	     * 邮件信息
	     */
		info.setToAddress(paramBean.getReceiver());
		info.setTitle(paramBean.getTitle());
		info.setContent(paramBean.getContent());
		
		EmailSender.sendMail(info);
		
		json.setSuccess(true);
		return json;
	}



	@Override
	public AjaxJson sendCloudMail(MailParamBean paramBean) {
        AjaxJson json=new AjaxJson();
		
		MailUser user=mailUserDao.findById(paramBean.getMailUserId());
		paramBean.setSmtpUserName(user.getSmtpUserName());
		paramBean.setSmtpPassword(user.getSmtpPassword());
		paramBean.setProtocol(user.getHost());
		paramBean.setPort(user.getPort());
		paramBean.setVirtualAddress("guokaige@sendcloud.com");
		paramBean.setVirtualName("郭凯歌");
		
		paramBean.setSendTime(new Date());
		
		//发邮件
		Boolean flag=SendCloudMailUtil.sendCommonMail(paramBean);
		json.setSuccess(flag);
		return json;
	}



}
